package com.ricchieri.manage.bean;

import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.PieChartModel;

import com.ricchieri.bean.ReservationOperatorBean;
import com.ricchieri.filter.ReservationFilter;
import com.ricchieri.service.interf.IReportService;
import com.ricchieri.service.interf.IReservationTypeService;
import com.ricchieri.service.interf.ITripService;

@ManagedBean(name = "reservationOperatorMB")
@ViewScoped
public class ReservationOperatorManageBean extends GenericManageBean {

	private static final long serialVersionUID = -275710803306101330L;
	private List <ReservationOperatorBean> reservationOperator;
	private PieChartModel pieModel;
	private CartesianChartModel categoryModel;
	private ReservationFilter filter = new ReservationFilter();
	
	private Map<String, String> reservationType;
	private Map<String, String> trips;
	
	@ManagedProperty(value = "#{ReportService}")
	IReportService reportService;
	
	@ManagedProperty(value = "#{ReservationTypeService}")
	IReservationTypeService reservationTypeService;
	
	@ManagedProperty(value = "#{TripService}")
	ITripService tripService;
	
	public IReportService getReportService() {
		return reportService;
	}

	public void setReportService(IReportService reportService) {
		this.reportService = reportService;
	}

	public List<ReservationOperatorBean> getReservationOperator() {
		if (reservationOperator == null) {
			int totalReservation = 0;
			reservationOperator = getReportService().getReservationByOperator(filter);
			for (ReservationOperatorBean reservationOperatorBean : getReservationOperator()) {
				totalReservation = totalReservation + reservationOperatorBean.getCount();
			}
			for (ReservationOperatorBean reservationOperatorBean : getReservationOperator()) {
				reservationOperatorBean.setPercentaje(Double.valueOf((reservationOperatorBean.getCount().doubleValue()/ totalReservation)*100));
			}
		}
		
		return reservationOperator;
	}

	public void setReservationOperator(
			List<ReservationOperatorBean> reservationOperator) {
		this.reservationOperator = reservationOperator;
	}

	public PieChartModel getPieModel() {
		if(pieModel == null) {
			pieModel = new PieChartModel();	
			for (ReservationOperatorBean reservationOperatorBean : getReservationOperator()) {
				pieModel.set(reservationOperatorBean.getUser().getFullName(), reservationOperatorBean.getCount());
			}
			if(pieModel.getData().isEmpty()){
				pieModel.set("", 0);
			}
		}
		return pieModel;  
	}

	public void setPieModel(PieChartModel pieModel) {
		this.pieModel = pieModel;
	}

	public void setCategoryModel(CartesianChartModel categoryModel) {
		this.categoryModel = categoryModel;
	}

	public CartesianChartModel getCategoryModel() {
		if(categoryModel == null) {
			categoryModel = new CartesianChartModel();
			ChartSeries chartSeries = new ChartSeries(); 
			for (ReservationOperatorBean reservationOperatorBean : getReservationOperator()) {
				chartSeries.set(reservationOperatorBean.getUser().getFullName(), reservationOperatorBean.getCount());
			}
			if(chartSeries.getData().isEmpty()){
				chartSeries.set("", 0);
			}
			
			categoryModel.addSeries(chartSeries);
		}
		return categoryModel;
	} 
	
	public void reset() {
		pieModel = null;
		categoryModel = null;
		reservationOperator = null;
		filter = new ReservationFilter();
	}
	
	public void search() {
		pieModel = null;
		categoryModel = null;
		reservationOperator = null;		
	}

	public ReservationFilter getFilter() {
		return filter;
	}

	public void setFilter(ReservationFilter filter) {
		this.filter = filter;
	}
	
	public Map<String, String> getTrips() {
		if(trips == null) {
			trips =  getTripService().getComboTrips();
		}
		return trips;
	}

	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}

	public Map<String, String> getReservationType() {
		if(reservationType == null) {
			reservationType = getReservationTypeService().getComboReservationType();
		}
		return reservationType;
	}

	public void setReservationType(Map<String, String> reservationType) {
		this.reservationType = reservationType;
	}

	public IReservationTypeService getReservationTypeService() {
		return reservationTypeService;
	}

	public void setReservationTypeService(
			IReservationTypeService reservationTypeService) {
		this.reservationTypeService = reservationTypeService;
	}

	public ITripService getTripService() {
		return tripService;
	}

	public void setTripService(ITripService tripService) {
		this.tripService = tripService;
	}
}

